package com.youlu.campus.web.data.english;


import com.youlu.campus.common.utils.excel.ExcelUtils;
import com.youlu.campus.entity.english.vo.request.ImportQaRankingRequest;
import com.youlu.campus.entity.english.vo.request.QaRankingQuery;
import com.youlu.campus.service.english.UserCompetitionRankingService;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
public class UserCompetitionRankingController {

    @Autowired
    private UserCompetitionRankingService userCompetitionRankingService;

    @PostMapping("/public/qa-import/ranking")
    public QResult importQaRanking(@RequestParam("file") MultipartFile file){
        ExcelUtils<ImportQaRankingRequest> excelUtil = new ExcelUtils();
        List<ImportQaRankingRequest> excelDataList = excelUtil.readExcel(file, ImportQaRankingRequest.class);
        if (CollectionUtils.isEmpty(excelDataList)) {
            return QResult.fail("导入文件错误");
        }
        userCompetitionRankingService.importQaRanking(excelDataList);
        return QResult.success(null);
    }




    @PostMapping("/mini-app/competition/qa/ranking")
    public QResult userQaRanking(@RequestBody @Validated QaRankingQuery qaRankingQuery, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
     return QResult.success(userCompetitionRankingService.userQaList(qaRankingQuery));
    }


    @GetMapping("/mini-app/competition/invite/ranking")
    public QResult userInviteRanking(@RequestParam String activityId,
                                     @RequestParam (value = "pageNo",defaultValue = "1") Integer pageNo,
                                     @RequestParam (value = "pageSize",defaultValue = "20") Integer pageSize){
        return QResult.success(userCompetitionRankingService.inviteRanking(activityId,pageNo,pageSize));
    }
}
